package com.ryan.statistic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ryan.statistic.entity.StatisticsDaily;
import com.ryan.statistic.feignclient.MemberClient;
import com.ryan.statistic.mapper.StatisticsDailyMapper;
import com.ryan.statistic.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ryan.utils.R;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author ryan
 * @since 2020-08-03
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private MemberClient memberClient;

    /**
     * 统计注册人数
     * @param day
     */
    @Override
    public void createRegisterData(String day) {
        //为保持数据最新，将原有的数据删掉
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("gmt_create",day);
        baseMapper.delete(wrapper);

        //获取最新数据
        R r = memberClient.countRegister(day);
        Integer registerCount = (Integer) r.getData().get("registerCount");
        //以下这些数据先伪造，具体操作都和registerCount一样的
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer routeViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer routeNum = RandomUtils.nextInt(100, 200);//TODO

        //最后设置并插入数据
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(registerCount);
        statisticsDaily.setLoginNum(loginNum);
        statisticsDaily.setRouteViewNum(routeViewNum);
        statisticsDaily.setRouteNum(routeNum);
        statisticsDaily.setDateCalculated(day);

        baseMapper.insert(statisticsDaily);
    }

    /**
     * 显示数据
     * @param type
     * @param begin
     * @param end
     * @return
     */
    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        //根据客户所选的数据，查询数据库并得到数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> staList = this.list(wrapper);

        //new一个map集合和两个list集合，准备座数据封装
        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        //封装数据
        //首先对查询出来的数据做遍历，根据客户选择的类型将数据封装到不同的集合中
        for (StatisticsDaily daily : staList) {
            //日期都是固定的，直接添加到date集合中即可
            dateList.add(daily.getDateCalculated());
            //判断客户选择要显示数据的类型
            switch (type){
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "route_view_num":
                    dataList.add(daily.getRouteViewNum());
                    break;
                case "route_num":
                    dataList.add(daily.getRouteNum());
                    break;
                default:
                    break;
            }
        }
        map.put("dataList",dataList);
        map.put("dateList",dateList);
        return map;
    }
}
